package com.codeyang.controller;

import com.alibaba.fastjson.JSON;
import com.codeyang.constant.QueueConstant;
import com.codeyang.model.WxMsgModel;
import com.codeyang.service.UserService;
import com.codeyang.util.LoadUserInfoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述: 短信发送控制层
 *
 * @author CodeYang_Site
 * @version 2021/5/30 16:41
 */
@RestController
@RequestMapping("p/sms")
@Api(tags = "发送短信验证码的接口")
public class SendCodeController {


    @Autowired
    private UserService userService;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 直接接收一个Map参数集合 mvc 会帮我们自动转换
     *
     * @param map
     * @return
     */
    @PostMapping("send")
    @ApiOperation("发送短信验证吗")
    public ResponseEntity<String> send(@RequestBody Map<String, String> map) {
        String msg = userService.sendCode(map);
        return ResponseEntity.ok(msg);
    }


    @PostMapping("savePhone")
    @ApiOperation("保存手机号")
    public ResponseEntity<String> savePhone(@RequestBody Map<String, String> map) {
        String userId = LoadUserInfoUtil.getUserId();
        String result = userService.savePhone(userId, map);
        return ResponseEntity.ok(result);
    }

    @GetMapping("sendWxMsg")
    @ApiOperation("测试发送微信消息")
    public ResponseEntity<String> sendWxMsg(String userId) {

        //实际操作中,应该是根据用户id 进行一系列的业务数据查询,和组装操作,最后与微信的模板信息对应组装数据

        // 1.组装消息
        WxMsgModel wxMsgModel = new WxMsgModel();
        wxMsgModel.setUrl("https://www.baidu.com"); // 设置详情地址
        wxMsgModel.setTopColor("#173177");
        wxMsgModel.setToUser(userId);
        //自己的模板
        wxMsgModel.setTemplateId("XazqNU4dtuXiVSDGebpC9bykQwDsN3kyK40DNWICohQ");

        //数据部分

        HashMap<String, Map<String, String>> data = new HashMap<>();
        data.put("user", WxMsgModel.builderData("老王", "#173177"));
        data.put("number", WxMsgModel.builderData("4556789907622", "#173177"));
        //声明格式化器
        DateTimeFormatter dfDateTime = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        //创建时间
        LocalDateTime dateTime = LocalDateTime.now();
        //格式化
        String formatTime = dfDateTime.format(dateTime);
        data.put("buyTime", WxMsgModel.builderData(formatTime, "#173177"));
        data.put("product", WxMsgModel.builderData("女朋友", "#173177"));
        data.put("time", WxMsgModel.builderData(formatTime, "#173177"));
        data.put("money", WxMsgModel.builderData("999,998,123.012", "#173177"));


        wxMsgModel.setData(data);
        // 发消息
        rabbitTemplate.convertAndSend(QueueConstant.WECHAT_MSG_QUEUE, JSON.toJSONString(wxMsgModel));
        return ResponseEntity.ok("你已经下单成功了，女朋友正在出库");
    }


}
